package com.stylefeng.guns.modular.pay.controller.api;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.base.Strings;
import com.stylefeng.guns.core.common.constant.Const;
import com.stylefeng.guns.core.util.AjaxRes;
import com.stylefeng.guns.modular.pay.exception.BusException;
import com.stylefeng.guns.modular.pay.model.MchReg;
import com.stylefeng.guns.modular.pay.model.Member;
import com.stylefeng.guns.modular.pay.service.IAliyunSmsService;
import com.stylefeng.guns.modular.pay.service.IMchRegService;
import com.stylefeng.guns.modular.pay.service.IMemberService;
import com.stylefeng.guns.modular.pay.service.IMessageService;
import com.stylefeng.guns.modular.pay.util.HideDataUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 会员信息相关接口
 * @author Administrator
 *
 */
@Api(value="会员相关业务接口",tags="会员相关业务接口")
@Controller
@RequestMapping("/api/member/")
public class ApiMemberController {
    private final static Logger logger = LoggerFactory.getLogger(ApiMemberController.class);

	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	@Autowired
	private IAliyunSmsService aliyunSmsService;
	@Autowired
	private IMemberService memberService;
	@Autowired
	private IMchRegService mchRegService;

	
	 @RequestMapping("")
	 public String index() {
	     return "/api/phone_bingding";
	 }
	 

	@ApiOperation(value = "手机绑定", notes = "手机绑定")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "smsCode", value = "短信验证码", required = true, dataType = "String"),
			@ApiImplicitParam(name = "memberId", value = "会员id", required = true, dataType = "String",paramType="path")
	})
	@RequestMapping(value = "/bindphone/{memberId}", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes bindphone(@RequestParam("phone")String phone,@RequestParam("smsCode")String smsCode,
			@PathVariable("memberId") Integer memberId) {
		if(Strings.isNullOrEmpty(phone)){
			return new AjaxRes("empty.phone", "手机号为空");
		}
		if(Strings.isNullOrEmpty(smsCode)){
			return new AjaxRes("empty.smsCode", "验证码为空");
		}
		String code = (String) redisTemplate.boundHashOps(Const.SMS_CODE).get(phone);
		if(!smsCode.equals(code)){
			return new AjaxRes("error.smsCode", "验证码错误");
		}
		//会员表更新绑定手机
		Member member = memberService.selectById(memberId);
		member.setPhone(phone);
		memberService.updateById(member);
		
		return new AjaxRes(AjaxRes.SUCCESS, "绑定成功");
	}
	
	
	@ApiOperation(value = "短信发送", notes = "短信发送")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String")})
	@RequestMapping(value = "/sendSms", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes sendSms(String phone) {
		if(Strings.isNullOrEmpty(phone)){
			return new AjaxRes("empty.phone", "手机号为空");
		}
		try {
			aliyunSmsService.sendSms(phone);
		} catch (BusException e) {
			logger.error(e.getRetCode()+"|"+e.getErrMsg());
			return new AjaxRes("sms.error",e.getErrMsg());
		}
		
		return new AjaxRes(AjaxRes.SUCCESS, "发送成功");
	}
	
	
	@ApiOperation(value = "我的账户信息", notes = "我的账户信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberId", value = "会员id", required = true, dataType = "String")})
	@RequestMapping(value = "/account/{memberId}", method = RequestMethod.GET)
	@ResponseBody
	public AjaxRes account(@PathVariable("memberId")Integer memberId) {
		Member member = memberService.selectById(memberId);
		MchReg mchReg = mchRegService.selectOne(new EntityWrapper<MchReg>().eq("mch_no", member.getMemberNo()));
		if(mchReg==null){
			return new AjaxRes("empty.mchReg", "商户尚未认证");
		}
		Map<String,String> dataMap = new HashMap<String,String>();
		dataMap.put("realName", mchReg.getRealName());
		dataMap.put("merName", mchReg.getMerName());
		dataMap.put("idCard", HideDataUtil.hideCardNo(mchReg.getIdCard()));
		dataMap.put("mobile", HideDataUtil.hidePhoneNo(mchReg.getMobile()));
		dataMap.put("bankName", mchReg.getBankName());
		dataMap.put("accountNo", HideDataUtil.hideCardNo(mchReg.getAccountNo()));
		dataMap.put("subBankCode", mchReg.getSubBankCode());
		
		return new AjaxRes(AjaxRes.SUCCESS, "查询成功",dataMap);
	}
	
	
	
	@ApiOperation(value = "个人信息", notes = "个人信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberId", value = "会员id", required = true, dataType = "String")})
	@RequestMapping(value = "/myinfo/{memberId}", method = RequestMethod.GET)
	@ResponseBody
	public AjaxRes myinfo(@PathVariable("memberId")Integer memberId) {
		Member member = memberService.selectById(memberId);
		if(member==null){
			return new AjaxRes("empty.member", "会员不存在");
		}
		MchReg mchReg = mchRegService.findByMchNo(member.getMemberNo());
		member.setRealName(mchReg.getRealName());
		member.setMerName(mchReg.getMerName());

		return new AjaxRes(AjaxRes.SUCCESS, "查询成功",member);
	}
	
	

	

}
